package com.filetest.mapper;

import com.filetest.model.FileMetadata;
import org.apache.ibatis.annotations.*;

import java.util.List;
/**
 * @author xhw
 */

@Mapper
public interface FileMapper {
    //插入文件元数据
    @Insert("INSERT INTO file_metadata (path, depth, size, upload_time, user_id, type) " +
            "VALUES (#{path}, #{depth}, #{size}, #{uploadTime}, #{userId}, #{type})")
    void insert(FileMetadata fileMetadata);

    //直接查询用户根目录下的文件和文件夹
    @Select("SELECT * FROM file_metadata WHERE user_id = #{userId} AND depth = 1")
    List<FileMetadata> findRootFilesByUserId(@Param("userId") Integer userId);

    //根据路径和深度查询用户目录下的文件和文件夹
    @Select("SELECT * FROM file_metadata WHERE user_id = #{userId} AND path LIKE '${logicPath}%' AND depth = #{depth}")
    List<FileMetadata> findFilesByPathAndDepth(@Param("userId") Integer userId,
                                               @Param("logicPath") String logicPath,
                                               @Param("depth") int depth);


    //通过用户ID和路径查找文件信息
    @Select("SELECT * FROM file_metadata WHERE user_id = #{userId} AND path = #{path}")
    FileMetadata findByUserIdAndPath(@Param("userId") Integer userId, @Param("path") String path);

    //按照用户ID和完整路径删除单个文件或目录
    @Delete("DELETE FROM file_metadata WHERE (user_id = #{userId} AND path = #{path}) " +
            "OR (path LIKE #{path} AND user_id = #{userId})")
    void deleteByUserIdAndPath(@Param("userId") Integer userId, @Param("path") String path);
}